package com.xjtu.conan.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xjtu.conan.entity.TheatreCommentsInfo;
import com.xjtu.conan.entity.TheatreImagesInfo;
import com.xjtu.conan.entity.TheatreInfo;
import com.xjtu.conan.entity.UserInfo;
import com.xjtu.conan.exception.MyException;
import com.xjtu.conan.mapper.TheatreCommentsInfoMapper;
import com.xjtu.conan.service.TheatreCommentsInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 剧场版评论表 服务实现类
 * </p>
 *
 * @author 一只黑猩猩
 * @since 2021-04-19
 */
@Service
public class TheatreCommentsInfoServiceImpl extends ServiceImpl<TheatreCommentsInfoMapper, TheatreCommentsInfo> implements TheatreCommentsInfoService {

    @Autowired
    private TheatreCommentsInfoMapper theatreCommentsInfoMapper;

    @Override
    public List<TheatreCommentsInfo> getAllTheatreCommentsInfo(int current, int size) {
        Page<TheatreCommentsInfo> page = new Page<>(current, size);
        theatreCommentsInfoMapper.selectPage(page, null);
        System.out.println(page.getTotal());
        return page.getRecords();
    }

    @Override
    public TheatreCommentsInfo getTheatreCommentByid(Long tcid) {
        TheatreCommentsInfo theatreComments = theatreCommentsInfoMapper.selectById(tcid);
        if (theatreComments == null) {
            throw new MyException(500, "不存在该剧场版评论！");
        }
        return theatreComments;
    }

    @Override
    public int addTheatreComments(TheatreCommentsInfo commentsInfo) throws IOException {
        try {
            return theatreCommentsInfoMapper.insert(commentsInfo);
        } catch (Exception e) {
            throw new MyException(500, "剧场版评论插入失败！");
        }
    }

    @Override
    public List<TheatreCommentsInfo> getTheatreCommentsBytid(Long tid) {
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("theatre_id", tid);
            return theatreCommentsInfoMapper.selectByMap(map);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(500, "该剧场版评论数为0！");
        }
    }

    @Override
    public List<TheatreCommentsInfo> getTheatreCommentsBypid(Long pid) {
        try {
            HashMap<String, Object> map = new HashMap<>();
            map.put("parent_id", pid);
            return theatreCommentsInfoMapper.selectByMap(map);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(500, "该评论的子评论数为0！");
        }
    }


    @Override
    public int updateTheatreComments(TheatreCommentsInfo commentsInfo) {
        try {
            return theatreCommentsInfoMapper.updateById(commentsInfo);
        } catch (Exception e) {
            throw new MyException(500, "剧场版评论更新失败！");
        }
    }


    @Override
    public int deleteTheatreComments(Long tcid) {
        try {
            return theatreCommentsInfoMapper.deleteById(tcid);
        } catch (Exception e) {
            e.printStackTrace();
            throw new MyException(500, "剧场版评论删除失败！");
        }

    }

    @Override
    public int getTotal() {
        return theatreCommentsInfoMapper.selectCount(null);

    }
}
